class TFThermoBlaster extends TFWeapon
    config(user);

var Pawn SeekTarget;
var float LockTime, UnLockTime, SeekCheckTime, AmmoCheckTime;
var bool bLockedOn, bBreakLock;
var bool bTightSpread;
var() float SeekCheckFreq, SeekRange;
var() float LockRequiredTime, UnLockRequiredTime;
var() float LockAim;
var() Color CrosshairColor;
var() float CrosshairX, CrosshairY;
var() vector SeekingLocation;

#exec obj load file=..\Sounds\MenuSounds.uax

replication
{
    reliable if (Role == ROLE_Authority && bNetOwner)
        bLockedOn;
}

function Tick(float dt)
{
    local Pawn Other;
    local Vector StartTrace;
    local Rotator Aim;
    local float BestDist, BestAim;

    if (Ammo[0].AmmoAmount < 100 && Level.TimeSeconds > AmmoCheckTime){		// Ammo[0].InitialAmount
	Ammo[0].AmmoAmount +=2;
	AmmoCheckTime = Level.TimeSeconds + 1;}

    if (Instigator == None || Instigator.Weapon != self)
        return;

	if ( Role < ROLE_Authority )
		return;

    if ( !Instigator.IsHumanControlled() )
        return;

    if (Level.TimeSeconds > SeekCheckTime)
    {
        if (bBreakLock)
        {
            bBreakLock = false;
            bLockedOn = false;
            SeekTarget = None;
        }

        StartTrace = Instigator.Location + Instigator.EyePosition();
        Aim = Instigator.GetViewRotation();

        BestAim = LockAim;
        Other = Instigator.Controller.PickTarget(BestAim, BestDist, Vector(Aim), StartTrace, SeekRange);

        if ( CanLockOnTo(Other) )
        {
            if (Other == SeekTarget)
            {
                LockTime += SeekCheckFreq;
                if (!bLockedOn && LockTime >= LockRequiredTime)
                {
                    bLockedOn = true;
//                    PlayerController(Instigator.Controller).ClientPlaySound(Sound'WeaponSounds.LockOn');
                    PlayerController(Instigator.Controller).ClientPlaySound(Sound'MenuSounds.J_MouseOver');
		    SeekingLocation=SeekTarget.Location;
                 }
            }
            else
            {
                SeekTarget = Other;
                LockTime = 0.0;
            }
            UnLockTime = 0.0;
        }
        else
        {
            if (SeekTarget != None)
            {
                UnLockTime += SeekCheckFreq;
                if (UnLockTime >= UnLockRequiredTime)
                {
                    SeekTarget = None;
                    if (bLockedOn)
                    {
                        bLockedOn = false;
//                        PlayerController(Instigator.Controller).ClientPlaySound(Sound'WeaponSounds.SeekLost');
                        PlayerController(Instigator.Controller).ClientPlaySound(Sound'MenuSounds.J_MouseOver');
			SeekingLocation=vect(0,0,0);
                    }
                }
            }
            else
                 bLockedOn = false;
         }

        SeekCheckTime = Level.TimeSeconds + SeekCheckFreq;
    }
}

function bool CanLockOnTo(Actor Other)
{
    local Pawn P;
    P = Pawn(Other);

    if (P == None || P == Instigator || !P.bProjTarget)
        return false;

    if (!Level.Game.bTeamGame)
        return true;

    return ( (P.PlayerReplicationInfo == None) || (P.PlayerReplicationInfo.Team != Instigator.PlayerReplicationInfo.Team) );
}

simulated event RenderOverlays( Canvas Canvas )
{
    local int m;
	local vector NewScale3D;
//	local rotator CenteredRotation;

    if (Instigator == None)
        return;

	if ( Instigator.Controller != None )
		Hand = Instigator.Controller.Handedness;

    if ((Hand < -1.0) || (Hand > 1.0))
        return;

    // draw muzzleflashes/smoke for all fire modes so idle state won't
    // cause emitters to just disappear
    Canvas.DrawActor(None, false, true); // amb: Clear the z-buffer here

    for (m = 0; m < NUM_FIRE_MODES; m++)
    {
        if (FireMode[m] != None)
        {
            FireMode[m].DrawMuzzleFlash(Canvas);
        }
    }

	Hand = -1;
	newScale3D = Default.DrawScale3D;
	SetDrawScale3D(newScale3D);

	PlayerViewPivot.Roll = Default.PlayerViewPivot.Roll * Hand;
	PlayerViewPivot.Yaw = Default.PlayerViewPivot.Yaw * Hand;

	PlayerViewOffset = Default.PlayerViewOffset;
	PlayerViewOffset.Y = -18;

    SetLocation( Instigator.Location + Instigator.CalcDrawOffset(self) );
    SetRotation( Instigator.GetViewRotation() );

    bDrawingFirstPerson = true;
    Canvas.DrawActor(self, false, false, DisplayFOV);
    bDrawingFirstPerson = false;


	Hand = 1;
	newScale3D = Default.DrawScale3D;
	SetDrawScale3D(newScale3D);

	PlayerViewPivot.Roll = Default.PlayerViewPivot.Roll * Hand;
	PlayerViewPivot.Yaw = Default.PlayerViewPivot.Yaw * Hand;

	PlayerViewOffset = Default.PlayerViewOffset;
	PlayerViewOffset.Y = 18;

    SetLocation( Instigator.Location + Instigator.CalcDrawOffset(self) );
    SetRotation( Instigator.GetViewRotation() );

    bDrawingFirstPerson = true;
    Canvas.DrawActor(self, false, false, DisplayFOV);
    bDrawingFirstPerson = false;


    PlayerViewOffset.Y = 0;

    if (bLockedOn)
    {
        Canvas.DrawColor = CrosshairColor;
        Canvas.DrawColor.A = 255;
        Canvas.Style = ERenderStyle.STY_Alpha;

        Canvas.SetPos(Canvas.SizeX*0.5-CrosshairX*2, Canvas.SizeY*0.5-CrosshairY*2);
        Canvas.DrawTile(Texture'SniperArrows', CrosshairX*4.0, CrosshairY*4.0, 0.0, 0.0, Texture'SniperArrows'.USize, Texture'SniperArrows'.VSize);
    }

//    super.RenderOverlays(Canvas);
}

function Projectile SpawnProjectile(Vector Start, Rotator Dir)
{
    local ThermoBlasterProjectile TBProj;
    local bot B;

//    bBreakLock = true;

	// decide if bot should be locked on
	B = Bot(Instigator.Controller);
	if ( (B != None) && (B.Skill > 2 + 5 * FRand()) && (FRand() < 0.6)
		&& (B.Target == B.Enemy) && (VSize(B.Enemy.Location - B.Pawn.Location) > 2000 + 2000 * FRand())
		&& (Level.TimeSeconds - B.LastSeenTime < 0.4) && (Level.TimeSeconds - B.AcquireTime > 1.5) )
	{
		bLockedOn = true;
		SeekTarget = B.Enemy;
	}

    if (bLockedOn && SeekTarget != None)
    {
        TBProj = Spawn(class'ThermoBlasterProjectile',,, Start, Dir);
        if(TBProj!=none) TBProj.Seeking=SeekTarget;
//        if ( B != None )
//        {
//			bLockedOn = false;
//			SeekTarget = None;
//		}
        return TBProj;
    }
    else
    {
        TBProj = Spawn(class'ThermoBlasterProjectile',,, Start, Dir);
		bLockedOn = false;
		SeekTarget = None;
        return TBProj;
    }
}



// AI Interface

/* BestMode()
choose between regular or alt-fire
*/
function byte BestMode()
{
	local float temp;

	temp = Frand();
	if (temp < 0.5)
		return 0;
	else
		return 1;

}

function float SuggestAttackStyle()
{
    return 1.2;
}

function float SuggestDefenseStyle()
{
    return -1.2;
}

function float RangedAttackTime()
{
	return 2;
}

function bool RecommendRangedAttack()
{
	return True;
}

function bool RecommendLongRangedAttack()
{
	return False;
}

function float RefireRate()
{
    return 0.15;
}


// End AI Interface

defaultproperties
{
     SeekCheckFreq=0.250000
     SeekRange=8000.000000
     UnLockRequiredTime=2.500000
     LockAim=0.996000
     CrossHairColor=(R=250,A=255)
     CrosshairX=16.000000
     CrosshairY=16.000000
     FireModeClass(0)=class'Taskforces.ThermoBlasterAltFire'
     FireModeClass(1)=class'Taskforces.ThermoBlasterFire'
     SelectAnim="Pickup"
     PutDownAnim="PutDown"
     SelectSound=Sound'WeaponSounds.RocketLauncher.RocketLauncherPlunger'
     SelectForce="SwitchToLightningGun"
     AIRating=0.350000
     CurrentRating=0.350000
     bNoAmmoInstances=False
     EffectOffset=(X=75.000000,Y=36.000000,Z=-9.000000)
     DisplayFOV=60.000000
     HudColor=(G=0)
     SmallViewOffset=(X=60.000000,Z=-14.500000)
     CenteredOffsetY=0.000000
     CenteredRoll=0
     InventoryGroup=9
     GroupOffset=9
     PickupClass=Class'Taskforces.ThermoBlasterPickup'
     PlayerViewOffset=(X=55.000000,Z=-14.500000)
     BobDamping=2.300000
     AttachmentClass=Class'Taskforces.ThermoBlasterAttachment'
     IconMaterial=Texture'TFMedia.General.ArkonHUD'
     IconCoords=(X1=128,Y1=407,X2=200,Y2=441)
     ItemName="Thermo Blasters"
     LightHue=25
     LightSaturation=140
     LightBrightness=95.000000
     LightRadius=4.000000
     LightPeriod=3
     DrawType=DT_StaticMesh
     StaticMesh=StaticMesh'TFMedia.ThermoBlaster3rd'
     DrawScale=0.600000
     Skins(0)=Texture'TFMedia.ThermoBlaster.BlasterTex0'
     UV2Texture=Shader'XGameShaders.WeaponShaders.WeaponEnvShader'
     AmmoRegenAmount(0)=10
     SkillCost=50
}
